﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.DataAccess;
using AutoMapper;

namespace Micrhard.FMCG.Server.DTO
{
    public static class FMCGServerMappingExtension
    {
        #region Company
        public static CompanyModel ToModel(this Company entity)
        {
            return Mapper.Map<CompanyModel>(entity);
        }

        public static Company ToEntity(this CompanyModel model)
        {
            return Mapper.Map<Company>(model);
        }
        #endregion

        #region Employee
        public static EmployeeModel ToModel(this Employee entity)
        {
            var model = Mapper.Map<EmployeeModel>(entity);
            model.BelongCompanyName = entity.Company.Name;
            return model;
        }

        public static Employee ToEntity(this EmployeeModel model)
        {
            return Mapper.Map<Employee>(model);
        }
        #endregion

        #region KA
        public static KAModel ToModel(this KA entity)
        {
            return Mapper.Map<KAModel>(entity);
        }

        public static KA ToEntity(this KAModel model)
        {
            return Mapper.Map<KA>(model);
        }
        #endregion

        #region Product
        public static ProductModel ToModel(this Product entity)
        {
            return Mapper.Map<ProductModel>(entity);
        }

        public static Product ToEntity(this ProductModel model)
        {
            return Mapper.Map<Product>(model);
        }
        #endregion

        #region SubKA
        public static SubKAModel ToModel(this SubKA entity)
        {
            var model = Mapper.Map<SubKAModel>(entity);
            model.KAName = entity.KA.Name;
            model.SalerEmployees = entity.ManagementScopes.Where(p => p.Range == (int)Range.Saler)
                .ToList().Select(t => t.Employee.ToModel()).ToList();
            model.MonitorEmployees = entity.ManagementScopes.Where(p => p.Range == (int)Range.Monitor || p.Range == (int)Range.Manager)
                .ToList().Select(t => t.Employee.ToModel()).ToList();
            var nowMonth = DateTime.Now.Date.Month;
            if (entity.ProtalSubKAPlans.Count > 0)
            {
                var cnt = entity.ProtalSubKAPlans.Count(w => w.CreateTime.Value.Date.Month == nowMonth);
                if (cnt > 0)
                    cnt = cnt - 1;
                model.CheckTimes = cnt;
            }
            return model;
        }

        public static SubKA ToEntity(this SubKAModel model)
        {
            return Mapper.Map<SubKA>(model);
        }
        #endregion

        #region PrdMapping
        public static PrductMappingKAPrdModel ToModel(this ProductSaleMapping entity)
        {
            return Mapper.Map<PrductMappingKAPrdModel>(entity);
        }

        public static ProductSaleMapping ToEntity(this PrductMappingKAPrdModel model)
        {
            return Mapper.Map<ProductSaleMapping>(model);
        }
        #endregion

        #region Daily
        public static DailyModel ToModel(this Daily entity)
        {
            var model = Mapper.Map<DailyModel>(entity);
            if (entity.DailyComments.Count > 0)
            {
                List<DailyCommentModel> cmt = new List<DailyCommentModel>();
                entity.DailyComments.ToList().ForEach(d =>
                {
                    cmt.Add(new DailyCommentModel
                    {
                        CommentID = d.CommentID,
                        Content = d.Content,
                        CreateTime = d.CreateTime.Value,
                        DailyID = d.DailyID.Value,
                        OperatorID = d.OperatorID.Value,
                        OperatorName = d.Employee.Name
                    });
                });
                model.ApproveComment = cmt;
                model.CanDeleteOrUpdate = false;
            }
            else
            {
                model.CanDeleteOrUpdate = true;
            }
            model.TommrowPlanProtalSubKA = entity.ProtalSubKAPlans.ToList().Select(t => t.ToModel()).ToList();
            return model;
        }

        public static Daily ToEntity(this DailyModel model)
        {
            return Mapper.Map<Daily>(model);
        }
        #endregion

        #region Notice
        public static NoticeModel ToModel(this Notice entity)
        {
            var model = Mapper.Map<NoticeModel>(entity);
            model.RecEmployees = entity.NoticeRecScopes.Select(e => e.Employee.ToModel()).ToList();
            return model;
        }

        public static Notice ToEntity(this NoticeModel model)
        {
            return Mapper.Map<Notice>(model);
        }
        #endregion

        #region ProductDisplayImages
        public static ProductDisplayImageModel ToModel(this ProductDisplayImage entity)
        {
            return Mapper.Map<ProductDisplayImageModel>(entity); ;
        }

        public static ProductDisplayImage ToEntity(this ProductDisplayImageModel model)
        {
            return Mapper.Map<ProductDisplayImage>(model);
        }
        #endregion

        #region CheckIn
        public static CheckinModel ToModel(this EmployeeCheckIn entity)
        {
            var model = Mapper.Map<CheckinModel>(entity);
            model.Employee = entity.Employee.ToModel();
            model.SubKA = entity.SubKA.ToModel();
            return model;
        }

        public static EmployeeCheckIn ToEntity(this CheckinModel model)
        {
            return Mapper.Map<EmployeeCheckIn>(model);
        }
        #endregion

        #region ProtalSubKAPlan
        public static ProtalSubKAPlanModel ToModel(this ProtalSubKAPlan entity)
        {
            var model = Mapper.Map<ProtalSubKAPlanModel>(entity);
            model.Name = entity.SubKA.Name;
            return model;
        }

        public static ProtalSubKAPlan ToEntity(this ProtalSubKAPlanModel model)
        {
            return Mapper.Map<ProtalSubKAPlan>(model);
        }
        #endregion

        #region IspTemplate
        public static IspTemplateModel ToModel(this CheckListTemplate entity)
        {
            return Mapper.Map<IspTemplateModel>(entity);
        }

        public static CheckListTemplate ToEntity(this IspTemplateModel model)
        {
            return Mapper.Map<CheckListTemplate>(model);
        }
        #endregion


        #region IspCategory
        public static IspCategoryModel ToModel(this CheckListTemplateCategory entity)
        {
            var model = Mapper.Map<IspCategoryModel>(entity);
            model.TemplateName = entity.CheckListTemplate.TemplateName;
            return model;
        }

        public static CheckListTemplateCategory ToEntity(this IspCategoryModel model)
        {
            return Mapper.Map<CheckListTemplateCategory>(model);
        }
        #endregion

        #region IspItem
        public static IspItemModel ToModel(this CheckListTemplateItem entity)
        {
            var model = Mapper.Map<IspItemModel>(entity);
            model.CategoryName = entity.CheckListTemplateCategory.Name;
            model.TemplateName = entity.CheckListTemplateCategory.CheckListTemplate.TemplateName;
            return model;
        }

        public static CheckListTemplateItem ToEntity(this IspItemModel model)
        {
            return Mapper.Map<CheckListTemplateItem>(model);
        }
        #endregion

        #region IspTemplateAnswer
        public static IspTemplateAnswerModel ToModel(this CheckListTemplateAnswer entity)
        {
            var model = Mapper.Map<IspTemplateAnswerModel>(entity);
            model.ItemName = entity.CheckListTemplateItem.Name;
            return Mapper.Map<IspTemplateAnswerModel>(entity);
        }

        public static CheckListTemplateAnswer ToEntity(this IspTemplateAnswerModel model)
        {
            return Mapper.Map<CheckListTemplateAnswer>(model);
        }
        #endregion

        #region Order
        public static OrderModel ToModel(this Order entity)
        {
            var model = Mapper.Map<OrderModel>(entity);
            if (entity.CustomerOrders.Count > 0)
            {
                model.Customer = entity.CustomerOrders.First().Customer.ToModel();
            }
            if (entity.KAOrders.Count > 0)
            {
                model.SubKA = entity.KAOrders.First().SubKA.ToModel();
            }
            return model;
        }

        public static Order ToEntity(this OrderModel model)
        {
            return Mapper.Map<Order>(model);
        }
        #endregion

        #region OrderDetail
        public static OrderDetailModelInServer ToModel(this OrderDetail entity)
        {
            return Mapper.Map<OrderDetailModelInServer>(entity);
        }

        public static OrderDetail ToEntity(this OrderDetailModelInServer model)
        {
            return Mapper.Map<OrderDetail>(model);
        }
        #endregion

        #region News
        public static NewsModel ToModel(this News entity)
        {
            return Mapper.Map<NewsModel>(entity);
        }

        public static News ToEntity(this NewsModel model)
        {
            return Mapper.Map<News>(model);
        }
        #endregion

        #region Statement
        public static StatementModelInServer ToModel(this SaleStatement entity)
        {
            return Mapper.Map<StatementModelInServer>(entity);
        }

        public static SaleStatement ToEntity(this StatementModelInServer model)
        {
            return Mapper.Map<SaleStatement>(model);
        }
        #endregion

        #region ProductSaledOfDay
        public static ProductSaledOfDayModel ToModel(this ProductSaledOfDay entity)
        {
            return Mapper.Map<ProductSaledOfDayModel>(entity);
        }

        public static ProductSaledOfDay ToEntity(this ProductSaledOfDayModel model)
        {
            return Mapper.Map<ProductSaledOfDay>(model);
        }
        #endregion

        #region SaleLead
        public static SaleLeadModel ToModel(this SaleLead entity)
        {
            var model = Mapper.Map<SaleLeadModel>(entity);
            model.Customer = entity.Customer.ToModel();
            model.CustomerID = entity.Customer.CustomerID;
            model.Saler = entity.Customer.Employee.ToModel();
            //model.IntrOldCustomer = entity.OldCustomerIntr.ToModel();
            return model;
        }

        public static SaleLead ToEntity(this SaleLeadModel model)
        {
            return Mapper.Map<SaleLead>(model);
        }
        #endregion

        #region Customer
        public static CustomerModel ToModel(this Customer entity)
        {
            return Mapper.Map<CustomerModel>(entity);
        }

        public static Customer ToEntity(this CustomerModel model)
        {
            return Mapper.Map<Customer>(model);
        }
        #endregion

        #region FakeCode
        public static FakeCodeModel ToModel(this FakeCode entity)
        {
            return Mapper.Map<FakeCodeModel>(entity);
        }

        public static FakeCode ToEntity(this FakeCodeModel model)
        {
            return Mapper.Map<FakeCode>(model);
        }
        #endregion

        #region OrderWorkSheet
        public static OrderWorkSheetModel ToModel(this OrderWorkSheet entity)
        {
            var model = Mapper.Map<OrderWorkSheetModel>(entity);
            model.Order = entity.Order.ToModel();
            return model;
        }

        public static OrderWorkSheet ToEntity(this OrderWorkSheetModel model)
        {
            return Mapper.Map<OrderWorkSheet>(model);
        }
        #endregion

        #region CustomerCoin
        public static CustomerCoinModel ToModel(this CustomerCoin entity)
        {
            var model = Mapper.Map<CustomerCoinModel>(entity);
            if (entity.CustomerCoinOrders.Count > 0)
            {
                model.HistoryOrders = entity.CustomerCoinOrders.Select(p => p.ToModel()).ToList();
            }
            return model;
        }

        public static CustomerCoin ToEntity(this CustomerCoinModel model)
        {
            return Mapper.Map<CustomerCoin>(model);
        }
        #endregion

        #region CustomerCoinOrder
        public static CoinCusumeOrderModel ToModel(this CustomerCoinOrder entity)
        {
            var model = Mapper.Map<CoinCusumeOrderModel>(entity);
            model.Order = entity.CustomerOrder.Order.ToModel();
            return model;

        }

        public static CustomerCoinOrder ToEntity(this CoinCusumeOrderModel model)
        {
            return Mapper.Map<CustomerCoinOrder>(model);
        }
        #endregion
    }
}
